package com.hualan.sfzy.controller;

import com.hualan.sfzy.model.dto.PsychologyKnowledgeDto;
import com.hualan.sfzy.model.entity.PsychologyKnowledge;
import com.hualan.sfzy.model.vo.PsychologyKnowledgeQuery;
import com.hualan.sfzy.service.PsychologyKnowledgeService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.hualan.sfzy.constant.CommonConstant.*;

/**
 * 心理知识管理控制器
 */
@RestController
@RequestMapping("/psychologyKnowledge")

public class PsychologyKnowledgeController {

    @Autowired
    private PsychologyKnowledgeService psychologyKnowledgeService;

    /**
     * 添加心理知识
     */
    @PostMapping("/add")
    public Result add(@RequestBody PsychologyKnowledge psychologyKnowledge) {
        int result = psychologyKnowledgeService.add(psychologyKnowledge);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 编辑心理知识
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody PsychologyKnowledge psychologyKnowledge) {
        int result = psychologyKnowledgeService.edit(psychologyKnowledge);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 删除心理知识
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        int result = psychologyKnowledgeService.remove(id);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 批量删除心理知识
     */
    @DeleteMapping("/removeBatch")
    public Result removeBatch(@RequestBody Integer[] ids) {
        int result = psychologyKnowledgeService.removeBatch(ids);
        return result > 0 ? Result.success() : Result.error();
    }

    /**
     * 查询所有心理知识
     */
    @GetMapping("/find")
    public Result find() {
        List<PsychologyKnowledgeDto> psychologyKnowledges = psychologyKnowledgeService.findAll();
        return Result.success(psychologyKnowledges);
    }

    /**
     * 分页查询心理知识
     */
    @GetMapping("/findPage")
    public Result findPage(@RequestParam(value = PAGE_NUM, defaultValue = PAGE_NUM_DEFAULT) Integer pageNum,
                          @RequestParam(value = PAGE_SIZE, defaultValue = PAGE_SIZE_DEFAULT) Integer pageSize,
                          PsychologyKnowledge psychologyKnowledge) {
        Map<String, Object> result = psychologyKnowledgeService.findPage(pageNum, pageSize, psychologyKnowledge);
        return Result.success(result);
    }

    /**
     * 根据ID查询心理知识
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        PsychologyKnowledge psychologyKnowledge = psychologyKnowledgeService.findById(id);
        return Result.success(psychologyKnowledge);
    }

    /**
     * 分页条件查询心理知识
     */
    @GetMapping("/findKnowledgePage")
    public Result findKnowledgePage(@RequestParam(value = PAGE_NUM, defaultValue = PAGE_NUM_DEFAULT) Integer pageNum,
                                   @RequestParam(value = PAGE_SIZE, defaultValue = PAGE_SIZE_DEFAULT) Integer pageSize,
                                   PsychologyKnowledgeQuery query) {
        Map<String, Object> result = psychologyKnowledgeService.findKnowledgePage(pageNum, pageSize, query);
        return Result.success(result);
    }
} 